#!/bin/bash
#
# Now that Youtube has gone completely nonfree, we need some way to watch it
# with a full FS stack, don't we?
#
# Tor is now optional (but recommended nonetheless), and yad is used instead of
# zenity to provide the GUI. It's a small download (<100kB) and has much better
# handling then zenity, although it doesn't always come by default.
#
# Note: requires mpv to work. Also, tor{socks} is crucial lest you not
# want to anonymize yourself
#
# CREDITS:
# Thanks goes to Jookia for pointing out how to do this in a neat one-liner:
# mpv --keep-open=yes --ytdl-format "bestvideo[protocol!=http_dash_segments][height<=?480]+bestaudio/best[height<=?480]" "$1"
#
# Basically all I did was wrap a GUI around the whole thing.
#
# LICENSE:
# Copyright 2016 Klaus Zimmermann - https://quitter.se/kzimmermann
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# Privacy is your unalienable right, but you should always have a choice.
# Change to 0 to stream directly without Tor (and give away your IP address):
TOR=1
YAD=0

if [[ -z $(which yad) ]]
then
    if [[ -z $(which zenity) ]]
    then
        echo "Warning: zenity is not installed in this system, cannot load the GUI"
        echo "You can still \"surf\" from the command-line, though."
        echo "Just run \`$(basename $0) [video url]\` to watch in private."
        exit 1
    else
        zenity --question --text="yad is not installed, which means that some features won't work.\nPlease install yad to get the full experience!" || exit 0
    fi
else
    YAD=1
fi


if [[ -z $(which mpv) ]]
then
    echo "This program requires mpv to work."
    echo "Please install it from your package manager and try again."
    exit 1
fi

while [[ true ]]
do
    DATA=$(yad --title "view-youtube menu" --form \
            --text="Enter the URL from YouTube to stream directly in the desktop. You can choose to use Tor as well."\
            --field "URL to stream" \
            --field "Use Tor:CHK"
            ) || exit 0

    [[ -z "$DATA" ]] && continue
    URL=$(echo "$DATA" | cut -d "|" -f 1)
    TOR=$(echo "$DATA" | cut -d "|" -f 2)
    if [[ "$TOR" == "TRUE" ]]
    then
        torsocks \
            mpv --keep-open=yes "$URL" &> /dev/null ||
                zenity --error \
                    --title "Video could not be streamed." \
                    --text "Error: [libav] https: HTTP error 403 Forbidden.\nWell, thank YouTube for the shitty API they use."
                # --ytdl-format "bestvideo[protocol!=http_dash_segments][height<=?480]+bestaudio/best[height<=?480]" "$1"
                # for some reason this doesn't seem to work..
    else
        mpv --keep-open=yes "$URL"
    fi
done
